package com.eshop.controller;

import com.eshop.model.CategoryDetail;
import com.eshop.model.Pic;
import com.eshop.service.*;
import com.eshop.service.Impl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/goods")
public class GoodsController extends HttpServlet {
    HeaderService headerService = new HeaderServiceImpl();
    GoodsCateService goodsCateService = new GoodsCateServiceImpl();
    GoodsDetailService goodsDetailService = new GoodsDetailServiceImpl();
    NewGoodsService newGoodsService = new NewGoodsServiceImpl();
    SaleGoodsService saleGoodsService = new SaleGoodsServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String action = req.getParameter("action");

        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void goodsCate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<CategoryDetail> categoryDetailListForHeader = headerService.getCateList();
        List<com.eshop.model.Goods> goodsList = goodsCateService.getGoodsListByCateId(Integer.parseInt(req.getParameter("cateId")));

        req.setAttribute("categoryDetailListForHeader",categoryDetailListForHeader);
        req.setAttribute("goodsList",goodsList);

        req.getRequestDispatcher("/pages/goods/goods_cate.jsp").forward(req, resp);
    }

    protected void goodsSort(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cateId = req.getParameter("cateId");
        String searchContent = req.getParameter("searchContent");
        String sort = req.getParameter("sort");

        if (cateId.trim().length() == 0 && searchContent.trim().length() == 0) {
            resp.sendRedirect(req.getContextPath()+"/home?action=index");
            return;
        }

        List<CategoryDetail> categoryDetailListForHeader = headerService.getCateList();
        List<com.eshop.model.Goods> goodsList;


        if (cateId.trim().length() == 0) {
            goodsList = goodsCateService.fuzzyEnquirySort(searchContent, sort);
        }
        else {
            goodsList = goodsCateService.getGoodsListByCateIdSort(Integer.parseInt(cateId), sort);
        }

        req.setAttribute("goodsList",goodsList);
        req.setAttribute("categoryDetailListForHeader",categoryDetailListForHeader);


        req.getRequestDispatcher("/pages/goods/goods_cate.jsp").forward(req, resp);
    }

    protected void goodsDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int goodsId =  Integer.parseInt(req.getParameter("goodsId"));

            com.eshop.model.Goods goods = goodsDetailService.getGoodsById(goodsId);
            List<Pic> picList = goodsDetailService.getPicListByGoodsId(goodsId);
            String parentName = goodsDetailService.getParentNameByCateId(goods.getCate_id());
            List<CategoryDetail> categoryDetailListForHeader = headerService.getCateList();

            req.setAttribute("goods",goods);
            req.setAttribute("picList",picList);
            req.setAttribute("parentName",parentName);
            req.setAttribute("categoryDetailListForHeader",categoryDetailListForHeader);

            req.getRequestDispatcher("/pages/goods/goods_detail.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void newGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<CategoryDetail> categoryDetailListForHeader = headerService.getCateList();
        List<com.eshop.model.Goods> newGoodsList = newGoodsService.getGoodsByLatestDate();

        req.setAttribute("categoryDetailListForHeader",categoryDetailListForHeader);
        req.setAttribute("newGoodsList",newGoodsList);

        req.getRequestDispatcher("/pages/goods/new_goods.jsp").forward(req, resp);
    }

    protected void saleGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<CategoryDetail> categoryDetailListForHeader = headerService.getCateList();
        List<com.eshop.model.Goods> hotGoodsList = saleGoodsService.getGoodsBySales(1);

        req.setAttribute("categoryDetailListForHeader",categoryDetailListForHeader);
        req.setAttribute("hotGoodsList",hotGoodsList);

        req.getRequestDispatcher("/pages/goods/sale_goods.jsp").forward(req, resp);
    }

    protected void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String searchContent = req.getParameter("searchContent");

        List<CategoryDetail> categoryDetailListForHeader = headerService.getCateList();
        List<com.eshop.model.Goods> goodsList = goodsCateService.fuzzyEnquiry(searchContent);

        req.setAttribute("categoryDetailListForHeader",categoryDetailListForHeader);
        req.setAttribute("goodsList",goodsList);

        req.getRequestDispatcher("/pages/goods/goods_cate.jsp").forward(req, resp);
    }
}
